#include "epoll.h"

Epoll::Epoll(int events) : events_(events)
{
    epfd_ = epoll_create(512);
    assert(epfd_ >= 0);
}

Epoll::~Epoll()
{
    close(epfd_);
}

bool Epoll::AddFd(int fd, uint32_t event)
{
    if (fd < 0)
        return false;
    struct epoll_event ev;
    ev.data.fd = fd;
    ev.events = event;
    return epoll_ctl(epfd_, EPOLL_CTL_ADD, fd, &ev) == 0;
}

bool Epoll::DelFd(int fd, uint32_t event)
{
    if (fd < 0)
        return false;
    return epoll_ctl(epfd_, EPOLL_CTL_DEL, fd, nullptr) == 0;
}

bool Epoll::ModFd(int fd,uint32_t event)
{
    if(fd<0) return false;
    struct epoll_event ev;
    ev.data.fd = fd;
    ev.events = event;
    return epoll_ctl(epfd_,EPOLL_CTL_MOD,fd,&ev);
}

int Epoll::Wait(int timeout)
{
    return epoll_wait(epfd_,&events_[0],static_cast<int>(events_.size()),timeout);
}

int Epoll::GetEventFd(size_t i) const
{
    return events_[i].data.fd;
}

uint32_t Epoll::GetEvents(size_t i) const
{
    return events_[i].events;
}